package com.example.musicplayer;


import android.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by asd on 2016/8/7.
 */
public class Fragment_3 extends Fragment implements View.OnClickListener {
    private MyData mData;
    private List<MyData> data;
    private ListView lv;
    private String[] mTitle = {"这个城市，晚安", "夏季中吹过的一阵清风", "欧美极致好听音乐"};
    private String[] mJieShao = {"这个城市，晚安", "夏季中吹过的一阵清风", "欧美极致好听音乐"};
    private int[] img = {R.drawable.a, R.drawable.d, R.drawable.f};

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_3, null);
        adapterData();
        init(view);
        return view;
    }

    private void adapterData() {

        data = new ArrayList<>();
        for (int i = 0; i < img.length; i++) {
            mData = new MyData();
            mData.setTitle(mTitle[i]);
            mData.setJieShao(mJieShao[i]);
            mData.setImg(img[i]);
            mData.setCount(i + 1);
            data.add(mData);
        }

    }

    private void init(View view) {
        view.findViewById(R.id.localMusic).setOnClickListener(this);
        view.findViewById(R.id.afterMusic).setOnClickListener(this);
        view.findViewById(R.id.downloadMusic).setOnClickListener(this);
        view.findViewById(R.id.loveMusic).setOnClickListener(this);
        lv = (ListView) view.findViewById(R.id.lv);
        MyAdapter adapter = new MyAdapter();
        lv.setAdapter(adapter);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.localMusic:
                Intent intent = new Intent(v.getContext(),LocalMusic.class);
                startActivity(intent);
                break;
            case R.id.afterMusic:
                Log.v("msg","bbbbbbbbbbbbbbbbb");
                break;
            case R.id.downloadMusic:
                Log.v("msg","ccccccccccccccccccc");
                break;
            case R.id.loveMusic:
                Log.v("msg","dddddddddddddddd");
                break;
        }
    }

    private class MyAdapter extends BaseAdapter {

        @Override
        public int getCount() {

            return data.size();
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder = new ViewHolder();
            if (convertView == null) {
                convertView = View.inflate(getActivity(), R.layout.data, null);
                holder.iv = (ImageView) convertView.findViewById(R.id.iv_data);
                holder.tv_title = (TextView) convertView.findViewById(R.id.tv_title);
                holder.tv_count = (TextView) convertView.findViewById(R.id.tv_Count);
                holder.tv_jieshao = (TextView) convertView.findViewById(R.id.tv_jieshao);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
            holder.iv.setImageResource(data.get(position).getImg());
            holder.tv_title.setText(data.get(position).getTitle());
            holder.tv_count.setText(data.get(position).getCount() + "首歌");
            holder.tv_jieshao.setText(data.get(position).getJieShao());
            return convertView;
        }

        private class ViewHolder {
            TextView tv_title;
            TextView tv_count;
            TextView tv_jieshao;
            ImageView iv;
        }
    }

}
